<?php

/**
 * default actions.
 *
 * @package    sf_sandbox
 * @subpackage default
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class defaultActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  }
 
  public function executeLogin(sfWebRequest $request)
  {
  		if ($this->getUser()->isAuthenticated())
  			$this->redirect('@homepage');
		$this->form = new UsersForm();
		
		if ($request->isMethod('POST'))
		{
			$sLogin = $request->getPostParameter('login');
			$sPass = $request->getPostParameter('mdp');
			$sAction = $request->getPostParameter('action');

			if ($sAction == 'new-user')
			{
				$this->form->bind($request->getParameter($this->form->getName()));
				if($this->form->isValid())
				{
					$oUser = $this->form->save();
					$oUser->ip = $_SERVER['REMOTE_ADDR'];
					$oUser->date_inscription = date('Y-m-d H:i:s');
					$oUser->save();

					//envoi du mail
					SendEmail::sendEmailConfirmation($oUser->id);

					// On connecte l'utilisateur
					sfContext::getInstance()->getUser()->connect($oUser);
				}
				// FIXME Message d'erreur
			}
			else
			{
				// Login
				if(!sfContext::getInstance()->getUser()->login($sLogin, $sPass))
					echo 'erreur';
				
			}		
			
		}
  }
}
